﻿using System;
namespace Producers.Kis.Xtee.Riik.Ee.Producer.Kis {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="MKMessage", Namespace="http://producers.kis.xtee.riik.ee/producer/kis", Target=typeof(MKMessage))]
    public class MKMessageSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IMKMessage clazz;
        
        public MKMessageSerializer(IMKMessage clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("BatchInfo");
            if (clazz.BatchInfo!=null) {
                global::Xtee.Core.SerializationMetadata metaBatchInfo = this.GetMetaData(clazz.BatchInfo);
                Writer.WriteTypeAttribute(metaBatchInfo.TypeQualifiedName.Namespace, metaBatchInfo.TypeQualifiedName.Name);
                metaBatchInfo.NewSerializer(clazz.BatchInfo).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : BatchInfo");
            }
            Writer.WriteEndElement();
            if (((clazz.Claim == null) 
                        || (clazz.Claim.Count < 1m))) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "Claim");
            }
            for (int x = 0; ((clazz.Claim != null) 
                        && (x < clazz.Claim.Count)); x = (x + 1)) {
                Writer.WriteStartElement("Claim");
                if (clazz.Claim[x]!=null) {
                    global::Xtee.Core.SerializationMetadata metaClaim = this.GetMetaData(clazz.Claim[x]);
                    Writer.WriteTypeAttribute(metaClaim.TypeQualifiedName.Namespace, metaClaim.TypeQualifiedName.Name);
                    metaClaim.NewSerializer(clazz.Claim[x]).WriteXml(Writer);
                }
                else {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Claim");
                }
                Writer.WriteEndElement();
            }
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="BatchInfo") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>MKMessage>BatchInfo");
                }
                global::Xtee.Core.SerializationMetadata metaBatchInfo = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IConsolidatedData>(Reader, "ConsolidatedData", "http://producers.kis.xtee.riik.ee/producer/kis");
                clazz.BatchInfo = metaBatchInfo.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IConsolidatedData>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("BatchInfo");
                    metaBatchInfo.NewSerializer(clazz.BatchInfo).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>MKMessage>BatchInfo", Reader.LocalName);
            }
            clazz.Claim = new System.Collections.Generic.List<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IClaim>();
            for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                        && (Reader.LocalName == "Claim")); ) {
                if (Reader.LocalName=="Claim") {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>MKMessage>Claim");
                    }
                    global::Xtee.Core.SerializationMetadata metaClaim = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IClaim>(Reader, "Claim", "http://producers.kis.xtee.riik.ee/producer/kis");
                    global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IClaim item = metaClaim.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IClaim>();
                    clazz.Claim.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("Claim");
                        metaClaim.NewSerializer(item).ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>MKMessage>Claim", Reader.LocalName);
                }
            }
            if ((clazz.Claim.Count < 1m)) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "Claim");
            }
        }
    }
}
